package cn.tedu.straw.redis.tag.controller;

import com.baomidou.mybatisplus.extension.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/v1/tags")
public class TagController {
    @Autowired
    RedisTemplate<String, Serializable> redisTemplate;
    //http://localhost:6301/v1/tags
    @GetMapping("")
    public R<List<Serializable>> getTagList(){
        //通过RedisTemplate 获取List类型数据的操作器
        ListOperations<String,Serializable> ops =redisTemplate.opsForList();
        //Redis中用于保存标签列表数据的key
        String tagsKey = "tags";
        //获取数据
        long start =0;
        long end =ops.size(tagsKey);
        List<Serializable> list = ops.range(tagsKey,start,end);
        //返回
        return R.ok(list);
    }
    @GetMapping("/{id}")
    public Serializable getTagInfo(@PathVariable Integer id){
        ValueOperations<String ,Serializable> ops = redisTemplate.opsForValue();
        String key = "tag"+ id;
        Serializable value = ops.get(key);
        return value;
    }
}
